package javolution.context;

import javolution.util.FastMap;

/* loaded from: classes2.dex */
public class LocalContext extends Context {
    private static Class CLASS = new LocalContext().getClass();
    final FastMap _references = new FastMap();

    /* loaded from: classes2.dex */
    public static class Reference<T> implements javolution.lang.Reference<T> {
        private T _defaultValue;
        private boolean _hasBeenLocallyOverriden;

        public Reference() {
            this(null);
        }

        public Reference(T t) {
            this._defaultValue = t;
        }

        private static LocalContext getLocalContext() {
            for (Context current = Context.getCurrent(); current != null; current = current.getOuter()) {
                if (current instanceof LocalContext) {
                    return (LocalContext) current;
                }
            }
            return null;
        }

        private T retrieveValue() {
            T t;
            for (Context current = Context.getCurrent(); current != null; current = current.getOuter()) {
                if ((current instanceof LocalContext) && (t = (T) ((LocalContext) current)._references.get(this)) != null) {
                    return t;
                }
            }
            return this._defaultValue;
        }

        @Override // javolution.lang.Reference
        public final T get() {
            return this._hasBeenLocallyOverriden ? retrieveValue() : this._defaultValue;
        }

        public T getDefault() {
            return this._defaultValue;
        }

        public T getLocal() {
            LocalContext localContext = getLocalContext();
            return localContext != null ? (T) localContext._references.get(this) : this._defaultValue;
        }

        @Override // javolution.lang.Reference
        public void set(T t) {
            LocalContext localContext = getLocalContext();
            if (localContext == null) {
                this._defaultValue = t;
                return;
            }
            localContext._references.put(this, t);
            this._hasBeenLocallyOverriden = true;
        }

        public void setDefault(T t) {
            this._defaultValue = t;
        }

        public String toString() {
            return String.valueOf(get());
        }
    }

    public static LocalContext enter() {
        return (LocalContext) Context.enter(CLASS);
    }

    public static LocalContext exit() {
        return (LocalContext) Context.exit();
    }

    @Override // javolution.context.Context
    protected void enterAction() {
    }

    @Override // javolution.context.Context
    protected void exitAction() {
        this._references.clear();
    }
}
